/*
 * File: SOTE.c
 *
 * Code generated for Simulink model 'SOTE'.
 *
 * Model version                  : 3.169
 * Simulink Coder version         : 9.4 (R2020b) 29-Jul-2020
 * C/C++ source code generated on : Tue Nov 14 09:29:59 2023
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: NXP->Cortex-M4
 * Code generation objective: MISRA C:2012 guidelines
 * Validation result: Not run
 */

#include "SOTE.h"
#include "SOTE_private.h"
#include "RTE.h"

/* Named constants for Chart: '<S2>/outint2' */
#define IN_ContChg                     ((uint8_T)1U)
#define IN_ContChg1                    ((uint8_T)2U)
#define IN_ContChg2                    ((uint8_T)3U)
#define IN_Init                        ((uint8_T)4U)
#define IN_Init1                       ((uint8_T)5U)
#define IN_Init2                       ((uint8_T)6U)
#define IN_Init3                       ((uint8_T)7U)

/* Block states (default storage) */
DW_SOTE_T SOTE_DW;

/* Real-time model */
static RT_MODEL_SOTE_T SOTE_M_;
RT_MODEL_SOTE_T *const SOTE_M = &SOTE_M_;

/* Exported data definition */

/* Definition for custom storage class: ExportToFile */
uint16_T sote_min_SChgRamainingTime = ((uint16_T)0U);/* '<S2>/outint2' */
uint16_T sote_min_chgTgtTime = ((uint16_T)0U);/* '<S2>/outint2' */

/* Forward declaration for local functions */
static uint16_T chgHeatTgtTimeCal(const uint16_T *Divide2);
static void inner_default_ContChg(void);
static void inner_default_ContChg1(void);

/* Function for Chart: '<S2>/outint2' */
static uint16_T chgHeatTgtTimeCal(const uint16_T *Divide2)
{
    uint16_T res;
    if (Rte_c_CellMinTmp_GetVal() <= 43)
    {
        res = (uint16_T)(((43 - Rte_c_CellMinTmp_GetVal()) << 2) + *Divide2);
    }
    else
    {
        res = *Divide2;
    }

    return res;
}

/* Function for Chart: '<S2>/outint2' */
static void inner_default_ContChg(void)
{
    int32_T tmp;
    uint16_T chanceTime;
    if (sote_min_chgTgtTime <= 5)
    {
        chanceTime = 240U;
    }
    else if (SOTE_DW.CM_ChargeTime > sote_min_chgTgtTime)
    {
        if (SOTE_DW.CM_ChargeTime > sote_min_chgTgtTime + SOTE_DW.tmp)
        {
            SOTE_DW.CM_ChargeTime = sote_min_chgTgtTime;
            chanceTime = 30U;
        }
        else
        {
            tmp = SOTE_DW.CM_ChargeTime - sote_min_chgTgtTime;
            chanceTime = (uint16_T)(tmp == 0 ? MAX_int32_T : 30 / tmp);
        }
    }
    else if (sote_min_chgTgtTime > SOTE_DW.CM_ChargeTime + SOTE_DW.tmp)
    {
        SOTE_DW.CM_ChargeTime = sote_min_chgTgtTime;
        chanceTime = 30U;
    }
    else
    {
        chanceTime = 300U;
    }

    if (sote_min_chgTgtTime < SOTE_DW.CM_ChargeTime)
    {
        SOTE_DW.cnt2 = 0U;
        SOTE_DW.cnt1++;
        if (SOTE_DW.cnt1 >= chanceTime + 10)
        {
            SOTE_DW.cnt1 = 0U;
            SOTE_DW.CM_ChargeTime--;
        }
    }
    else
    {
        if (sote_min_chgTgtTime >= SOTE_DW.CM_ChargeTime)
        {
            SOTE_DW.cnt1 = 0U;
            SOTE_DW.cnt2++;
            if (SOTE_DW.cnt2 >= 300)
            {
                SOTE_DW.cnt2 = 0U;
                SOTE_DW.CM_ChargeTime--;
            }
        }
    }

    if (SOTE_DW.CM_ChargeTime <= 1)
    {
        SOTE_DW.CM_ChargeTime = 1U;
        sote_min_SChgRamainingTime = 1U;
    }
    else if (SOTE_DW.CM_ChargeTime >= 600)
    {
        SOTE_DW.CM_ChargeTime = 600U;
        sote_min_SChgRamainingTime = SOTE_DW.CM_ChargeTime;
    }
    else
    {
        sote_min_SChgRamainingTime = SOTE_DW.CM_ChargeTime;
    }

    if (SOTE_DW.tmp > 30)
    {
        SOTE_DW.tmp = 60U;
    }
}

/* Function for Chart: '<S2>/outint2' */
static void inner_default_ContChg1(void)
{
    int32_T tmp;
    if (sopv_enum_SchgReqStep == 2)
    {
        if ((int32_T)(SOTE_DW.cntStep2 / 60U) < SOTE_DW.TgtTim2)
        {
            SOTE_DW.cntStep2++;
        }

        sote_min_chgTgtTime = (uint16_T)((SOTE_DW.TgtTim2 + SOTE_DW.TgtTim3) -
            (int32_T)(SOTE_DW.cntStep2 / 60U));
    }
    else
    {
        if (sopv_enum_SchgReqStep == 3)
        {
            if ((int32_T)(SOTE_DW.cntStep3 / 60U) < SOTE_DW.TgtTim3)
            {
                SOTE_DW.cntStep3++;
            }

            sote_min_chgTgtTime = (uint16_T)((SOTE_DW.TgtTim3 - (int32_T)
                (SOTE_DW.cntStep3 / 60U)) + 1);
        }
    }

    if (sote_min_chgTgtTime < SOTE_DW.CM_ChargeTime)
    {
        SOTE_DW.cnt2 = 0U;
        SOTE_DW.cnt1++;
        tmp = SOTE_DW.CM_ChargeTime - sote_min_chgTgtTime;
        if (SOTE_DW.cnt1 >= (uint16_T)((tmp == 0 ? MAX_int32_T : 30 / tmp) + 8))
        {
            SOTE_DW.cnt1 = 0U;
            SOTE_DW.CM_ChargeTime--;
        }
    }
    else
    {
        if (sote_min_chgTgtTime > SOTE_DW.CM_ChargeTime)
        {
            SOTE_DW.cnt1 = 0U;
            SOTE_DW.cnt2++;
            if (SOTE_DW.cnt2 >= 300)
            {
                SOTE_DW.cnt2 = 0U;
                SOTE_DW.CM_ChargeTime++;
            }
        }
    }

    if (SOTE_DW.CM_ChargeTime <= 1)
    {
        SOTE_DW.CM_ChargeTime = 1U;
        sote_min_SChgRamainingTime = 1U;
    }
    else if (SOTE_DW.CM_ChargeTime >= 420)
    {
        SOTE_DW.CM_ChargeTime = 420U;
    }
    else
    {
        sote_min_SChgRamainingTime = SOTE_DW.CM_ChargeTime;
    }
}

/* Output and update for function-call system: '<S1>/SOTE1' */
void SOTE1(void)
{
    int32_T Subtract2;
    int32_T i;
    uint32_T tmp;
    uint16_T Abs1;
    uint16_T Abs1_0;
    uint16_T Divide2;
    uint8_T si;
    boolean_T OR2;
    boolean_T guard1 = false;
    if ((int32_T)Rte_ma_BatCurr_GetVal() - 1000000 < -16000)
    {
        if (SOTE_DW.scnt < 10)
        {
            SOTE_DW.CurrArr[SOTE_DW.scnt] = (int32_T)Rte_ma_BatCurr_GetVal() -
                1000000;
            SOTE_DW.scnt++;
            si = 0U;
            Subtract2 = 0;
            while (si < SOTE_DW.scnt)
            {
                Subtract2 += SOTE_DW.CurrArr[si];
                si++;
            }

            Subtract2 = SOTE_DW.scnt == 0 ? Subtract2 >= 0 ? MAX_int32_T :
                MIN_int32_T : Subtract2 / SOTE_DW.scnt;
        }
        else
        {
            if (SOTE_DW.acnt >= 10)
            {
                SOTE_DW.acnt = 0U;
            }

            SOTE_DW.CurrArr[SOTE_DW.acnt] = (int32_T)Rte_ma_BatCurr_GetVal() -
                1000000;
            SOTE_DW.acnt++;
            si = 0U;
            Subtract2 = 0;
            while (si < 10)
            {
                Subtract2 += SOTE_DW.CurrArr[si];
                si++;
            }

            Subtract2 /= 10;
        }
    }
    else
    {
        Subtract2 = 0;
        for (i = 0; i < 10; i++)
        {
            SOTE_DW.CurrArr[i] = 0;
        }
    }

    if (Subtract2 < 0)
    {
        tmp = (uint32_T)-Subtract2;
        if ((uint32_T)-Subtract2 > 65535U)
        {
            tmp = 65535U;
        }

        Abs1 = (uint16_T)tmp;
    }
    else
    {
        tmp = (uint32_T)Subtract2;
        if ((uint32_T)Subtract2 > 65535U)
        {
            tmp = 65535U;
        }

        Abs1 = (uint16_T)tmp;
    }

    if (soce_pct_SOC < 10000)
    {
        tmp = (uint32_T)(Pccfg_BatRatedCap * 6 * (10000 - soce_pct_SOC) / 10);
        if (tmp <= 1000U)
        {
            tmp = 1000U;
        }
    }
    else
    {
        tmp = 1000U;
    }

    Divide2 = (uint16_T)(tmp / 16000U);
    OR2 = ((tmsc_enum_HeatSts == 2) || (tmsc_enum_HeatSts == 3) ||
           (tmsc_enum_HeatSts == 4));
    if (Abs1 == 0)
    {
        tmp = 9999999U;
    }
    else
    {
        if (Abs1 > 20000)
        {
            Abs1_0 = 20000U;
        }
        else if (Abs1 < 16000)
        {
            Abs1_0 = 16000U;
        }
        else
        {
            Abs1_0 = Abs1;
        }

        tmp = (uint16_T)(tmp / Abs1_0);
    }

    if (SOTE_DW.is_active_c6_SOTE == 0U)
    {
        SOTE_DW.is_active_c6_SOTE = 1U;
        SOTE_DW.is_c6_SOTE = IN_Init;
        SOTE_DW.CM_ChargeTime = 0U;
        sote_min_SChgRamainingTime = 0U;
        sote_min_chgTgtTime = 0U;
    }
    else
    {
        guard1 = false;
        switch (SOTE_DW.is_c6_SOTE)
        {
          case IN_ContChg:
            if (sopv_enum_SchgReqStep >= 2)
            {
                SOTE_DW.is_c6_SOTE = IN_ContChg1;
                SOTE_DW.cnt1 = 0U;
                SOTE_DW.cnt2 = 0U;
                SOTE_DW.TgtTim2 = 2U;
                SOTE_DW.TgtTim3 = 3U;
                SOTE_DW.cntStep2 = 0U;
                SOTE_DW.cntStep3 = 0U;
                inner_default_ContChg1();
            }
            else if (vewm_enum_SlowChgSts != 2)
            {
                SOTE_DW.is_c6_SOTE = IN_Init;
                SOTE_DW.CM_ChargeTime = 0U;
                sote_min_SChgRamainingTime = 0U;
                sote_min_chgTgtTime = 0U;
            }
            else if (OR2)
            {
                SOTE_DW.is_c6_SOTE = IN_ContChg2;
                SOTE_DW.cnt1 = 0U;
                SOTE_DW.cnt2 = 0U;
                sote_min_chgTgtTime = chgHeatTgtTimeCal(&Divide2);
            }
            else
            {
                SOTE_DW.tmp++;
                sote_min_chgTgtTime = (uint16_T)tmp;
                inner_default_ContChg();
            }
            break;

          case IN_ContChg1:
            if ((vewm_enum_SlowChgSts != 2) && (!OR2))
            {
                SOTE_DW.is_c6_SOTE = IN_Init;
                SOTE_DW.CM_ChargeTime = 0U;
                sote_min_SChgRamainingTime = 0U;
                sote_min_chgTgtTime = 0U;
            }
            else
            {
                inner_default_ContChg1();
            }
            break;

          case IN_ContChg2:
            OR2 = !OR2;
            if (OR2 && (Abs1 > 0))
            {
                SOTE_DW.is_c6_SOTE = IN_Init3;
                SOTE_DW.cnt = 0U;
            }
            else if ((vewm_enum_SlowChgSts != 2) && OR2)
            {
                SOTE_DW.is_c6_SOTE = IN_Init;
                SOTE_DW.CM_ChargeTime = 0U;
                sote_min_SChgRamainingTime = 0U;
                sote_min_chgTgtTime = 0U;
            }
            else if (sopv_enum_SchgReqStep >= 2)
            {
                SOTE_DW.is_c6_SOTE = IN_ContChg1;
                SOTE_DW.cnt1 = 0U;
                SOTE_DW.cnt2 = 0U;
                SOTE_DW.TgtTim2 = 2U;
                SOTE_DW.TgtTim3 = 3U;
                SOTE_DW.cntStep2 = 0U;
                SOTE_DW.cntStep3 = 0U;
                inner_default_ContChg1();
            }
            else
            {
                sote_min_chgTgtTime = chgHeatTgtTimeCal(&Divide2);
                if (sote_min_chgTgtTime < SOTE_DW.CM_ChargeTime)
                {
                    SOTE_DW.cnt2 = 0U;
                    SOTE_DW.cnt1++;
                    if (SOTE_DW.cnt1 >= 30)
                    {
                        SOTE_DW.cnt1 = 0U;
                        SOTE_DW.CM_ChargeTime--;
                    }
                }
                else
                {
                    if (sote_min_chgTgtTime >= SOTE_DW.CM_ChargeTime)
                    {
                        SOTE_DW.cnt1 = 0U;
                        SOTE_DW.cnt2++;
                        if (SOTE_DW.cnt2 >= 180)
                        {
                            SOTE_DW.cnt2 = 0U;
                            SOTE_DW.CM_ChargeTime--;
                        }
                    }
                }

                if (SOTE_DW.CM_ChargeTime <= 1)
                {
                    SOTE_DW.CM_ChargeTime = 1U;
                    sote_min_SChgRamainingTime = 1U;
                }
                else if (SOTE_DW.CM_ChargeTime >= 600)
                {
                    SOTE_DW.CM_ChargeTime = 600U;
                    sote_min_SChgRamainingTime = SOTE_DW.CM_ChargeTime;
                }
                else
                {
                    sote_min_SChgRamainingTime = SOTE_DW.CM_ChargeTime;
                }
            }
            break;

          case IN_Init:
            if (OR2)
            {
                SOTE_DW.is_c6_SOTE = IN_Init2;
                SOTE_DW.cnt = 0U;
            }
            else
            {
                if (vewm_enum_SlowChgSts == 2)
                {
                    SOTE_DW.is_c6_SOTE = IN_Init1;
                    SOTE_DW.cnt = 0U;
                }
            }
            break;

          case IN_Init1:
            if ((vewm_enum_SlowChgSts != 2) || OR2)
            {
                SOTE_DW.is_c6_SOTE = IN_Init;
                SOTE_DW.CM_ChargeTime = 0U;
                sote_min_SChgRamainingTime = 0U;
                sote_min_chgTgtTime = 0U;
            }
            else if ((SOTE_DW.cnt >= 2) && (Abs1 > 0) &&
                     ((Rte_c_CellMinTmp_GetVal() > 50) || (SOTE_DW.cnt > 10)))
            {
                SOTE_DW.CM_ChargeTime = (uint16_T)tmp;
                SOTE_DW.tmp = 20U;
                SOTE_DW.is_c6_SOTE = IN_ContChg;
                SOTE_DW.cnt1 = 0U;
                SOTE_DW.cnt2 = 0U;
                sote_min_chgTgtTime = (uint16_T)tmp;
                inner_default_ContChg();
            }
            else
            {
                SOTE_DW.cnt++;
            }
            break;

          case IN_Init2:
            if ((Rte_c_CellMinTmp_GetVal() <= 43) && (tmsc_enum_HeatSts == 2))
            {
                SOTE_DW.CM_ChargeTime = (uint16_T)(((43 -
                    Rte_c_CellMinTmp_GetVal()) << 2) + Divide2);
                guard1 = true;
            }
            else if (((Rte_c_CellMinTmp_GetVal() > 43) || (SOTE_DW.cnt > 10)) &&
                     (tmsc_enum_HeatSts == 3))
            {
                SOTE_DW.CM_ChargeTime = Divide2;
                guard1 = true;
            }
            else if (!OR2)
            {
                SOTE_DW.is_c6_SOTE = IN_Init;
                SOTE_DW.CM_ChargeTime = 0U;
                sote_min_SChgRamainingTime = 0U;
                sote_min_chgTgtTime = 0U;
            }
            else
            {
                SOTE_DW.cnt++;
            }
            break;

          default:
            /* case IN_Init3: */
            if (SOTE_DW.cnt > 10)
            {
                SOTE_DW.is_c6_SOTE = IN_ContChg;
                SOTE_DW.cnt1 = 0U;
                SOTE_DW.cnt2 = 0U;
                sote_min_chgTgtTime = (uint16_T)tmp;
                inner_default_ContChg();
            }
            else if (OR2)
            {
                SOTE_DW.is_c6_SOTE = IN_ContChg2;
                SOTE_DW.cnt1 = 0U;
                SOTE_DW.cnt2 = 0U;
                sote_min_chgTgtTime = chgHeatTgtTimeCal(&Divide2);
            }
            else
            {
                SOTE_DW.cnt++;
            }
            break;
        }

        if (guard1)
        {
            SOTE_DW.is_c6_SOTE = IN_ContChg2;
            SOTE_DW.cnt1 = 0U;
            SOTE_DW.cnt2 = 0U;
            sote_min_chgTgtTime = chgHeatTgtTimeCal(&Divide2);
        }
    }
}

/* Model step function */
void Task_SOTE(void)
{
    SOTE1();
}

/* Model initialize function */
void SOTE_initialize(void)
{
    /* (no initialization code required) */
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */
